package chenjiuxu.seniorui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;

/**
 * Created by JiuXu.C on 2017/12/5.
 * 流式布局
 */
public class FlowLayout extends ViewGroup {

    private ArrayList<Integer> lineH = new ArrayList<>();
    private ArrayList<ArrayList<View>> views = new ArrayList<>();
    int measuredWidth = -1;//测量完成宽
    int measuredHeight = -1;//测量完成高

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (measuredWidth != -1 && measuredHeight != -1) {
            setMeasuredDimension(measuredWidth, measuredHeight);
            return;
        }
        int iwMode = MeasureSpec.getMode(widthMeasureSpec);
        int ihMode = MeasureSpec.getMode(heightMeasureSpec);
        int iwidth = MeasureSpec.getSize(widthMeasureSpec);//最大宽度
        int iheight = MeasureSpec.getSize(heightMeasureSpec);//最大高度
        int tagLineW = 0;//宽记录
        int tagLineH = 0;//高记录
        int width, height;
        ArrayList<View> vl = new ArrayList<>();
        views.add(vl);
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View view = getChildAt(i);
            measureChild(view, widthMeasureSpec, heightMeasureSpec);
            MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
            width = view.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
            height = view.getMeasuredHeight() + layoutParams.topMargin + layoutParams.bottomMargin;
            if (width + tagLineW > iwidth) {//当记录宽度加 view宽度 大于最大宽度
                //换行
                measuredWidth = Math.max(measuredHeight, tagLineW);//测量完成宽 对比换行记录宽度 赋予最值给 测量完成宽
                measuredHeight += tagLineH;//累加高度
                lineH.add(tagLineH);//记录每一行高度

                tagLineW = width;//重置
                tagLineH = height;//重置

                vl = new ArrayList<>();
                views.add(vl);
                vl.add(view);
            } else {
                tagLineW += width;//记录宽
                tagLineH = Math.max(tagLineH, height);//记录每一行最大高度
                vl.add(view);
                if (i == count - 1) {//最后一行没有换行时
                    measuredHeight += tagLineH;//累加高度
                    lineH.add(tagLineH);//记录每一行高度
                    if (views.size() == 1) measuredWidth = tagLineW;
                }
            }
        }

        if (ihMode == MeasureSpec.EXACTLY) measuredHeight = iheight;
        if (iwMode == MeasureSpec.EXACTLY)measuredWidth = iwidth;
        setMeasuredDimension(measuredWidth, measuredHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int left, top, right, bottom;
        int ctop = t;//顶点
        int cleft = l;//顶点lt
        for (int i = 0; i < lineH.size(); i++) {
            int h = lineH.get(i);
            ArrayList<View> vl = views.get(i);
            for (int a = 0; a < vl.size(); a++) {
                View view = vl.get(a);
                MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
                left = cleft + layoutParams.leftMargin;
                top = ctop + layoutParams.topMargin;
                right = left + view.getMeasuredWidth();
                bottom = top + view.getMeasuredHeight();
                view.layout(left, top, right, bottom);
                cleft += view.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
            }
            cleft = 0;
            ctop += h;
        }

    }
}
